package com.nvn.washiodriver.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.Order;
import com.nvn.washiodriver.network.WebServiceCommunicatorListener;
import com.nvn.washiodriver.network.ws.BaseWSControl;
import com.nvn.washiodriver.network.ws.BaseWSControl.WebServiceFlag;
import com.nvn.washiodriver.network.ws.UpdateBagsWSControl;
import com.nvn.washiodriver.utils.SessionManager;
import com.nvn.washiodriver.utils.Statics;
import com.nvn.washiodriver.views.CustomDialog;

public class BagsActivity extends Activity implements OnClickListener,
		WebServiceCommunicatorListener {

	private TextView mTvLaundry, mTvDryClean, mTvCancel, mTvSave, mTvTitle;
	private CustomDialog myDialogLaundry, myDialogDryClean;
	private int mPosLaundry, mPosDryClean;
	private Order mOrder;
	private ProgressDialog pd;
	private ArrayList<Object> arrLaundry, arrDryClean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pickup);

		initView();
		initProperties();
		initListeners();
	}

	protected void initView() {
		mTvLaundry = (TextView) findViewById(R.id.tv_laundry);
		mTvDryClean = (TextView) findViewById(R.id.tv_dry_clean);

		mTvSave = (TextView) findViewById(R.id.tv_save);
		mTvCancel = (TextView) findViewById(R.id.tv_cancel);
		mTvTitle = (TextView) findViewById(R.id.tv_title_bar);
	}

	private void initProperties() {
		arrLaundry = new ArrayList<Object>();
		arrDryClean = new ArrayList<Object>();
		mOrder = SessionManager.getSessionManager(this).getCurOrder();
		if (mOrder == null) {
			Statics.showToast(this, "This order is not exist!");
			finish();
		}

		for (int i = 1; i <= 20; i++) {
			arrLaundry.add(i);
			arrDryClean.add(i);
		}

		mTvDryClean.setText(mOrder.getDryCleanCout() + "");
		mTvLaundry.setText(mOrder.getLaundryCount() + "");

		mPosDryClean = mOrder.getDryCleanCout() - 1;
		mPosLaundry = mOrder.getLaundryCount() - 1;

		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Loading...");

	}

	private void initListeners() {
		mTvLaundry.setOnClickListener(this);
		mTvDryClean.setOnClickListener(this);
		mTvSave.setOnClickListener(this);
		mTvCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.tv_laundry) {
			showDialogLaundry();
		} else if (v.getId() == R.id.tv_dry_clean) {
			showDialogDryClean();
		} else if (v.getId() == R.id.tv_save) {
			new UpdateBagsWSControl(this, this, mOrder.getObjectId(),
					mTvLaundry.getText().toString(), mTvDryClean.getText()
							.toString()).execute();
		} else if (v.getId() == R.id.tv_cancel) {
			finish();
		}
	}

	public void showDialogLaundry() {

		myDialogLaundry = new CustomDialog(this, arrLaundry, getResources()
				.getString(R.string.txt_laundry), mPosLaundry);
		myDialogLaundry.setOnClickItem(new CustomDialog.OnClickItem() {

			@Override
			public void Click(int position) {
				// TODO Auto-generated method stub
				position -= 1;
				mTvLaundry.setText(arrLaundry.get(position).toString());
				mPosLaundry = position;

			}
		});
		myDialogLaundry.show();
	}

	private void showDialogDryClean() {
		myDialogDryClean = new CustomDialog(this, arrDryClean, getResources()
				.getString(R.string.txt_dry_clean), mPosDryClean);
		myDialogDryClean.setOnClickItem(new CustomDialog.OnClickItem() {

			@Override
			public void Click(int position) {
				// TODO Auto-generated method stub
				position -= 1;
				mTvDryClean.setText(arrDryClean.get(position).toString());
				mPosDryClean = position;

			}
		});
		myDialogDryClean.show();
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		pd.show();
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		Statics.showToast(this, error);
		pd.dismiss();
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		if (flag == WebServiceFlag.UPDATE_BAGS) {
			Statics.showToast(this, result.toString());
			Intent i = getIntent();
			i.putExtra("LAUNDRY", Integer.parseInt(mTvLaundry.getText().toString()));
			i.putExtra("DRYCLEAN", Integer.parseInt(mTvDryClean.getText().toString()));
			setResult(RESULT_OK, i);
			finish();
		}
		pd.dismiss();
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
	}
}
